package client.comm.baoding.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import client.comm.baoding.R;
import client.comm.baoding.base.BaseDialogFragment;
import client.comm.baoding.ui.TextContentActivity;
import client.comm.baoding.utils.HandlerCode;
import client.comm.baoding.utils.ReadAssetsUtil;
import client.comm.baoding.utils.ScreenUtil;
import client.comm.baoding.widget.MaxHeightScrollView;


/**
 * Created by Administrator on 2019/7/16 0016.
 */

public class ProtocolDialogFragment extends BaseDialogFragment {


    MaxHeightScrollView scrollView;
    TextView tvProtcalTitle;
    TextView protcalDialogContent;
    TextView btnProtcalLeft;
    TextView btnProtcalRight;

    private String title, content, leftBtn, rightBtn;
    private OnOperationConfirmListen mOnOperationConfirmListen;
    private OnOperationCancelListen mOnOperationCancelListen;
    private int jumpType = -1;
    private String registerContent;

    @Override
    protected void handleMsg(Message msg) {
        switch (msg.what) {
//            case HandlerCode.SUCCESS:
//                NewsResponse<String> result = (NewsResponse<String>) msg.obj;
//                switch (msg.arg1) {
//                    case HandlerCode.GET_APP_PARAM:
//                        ParamsBean paramsBean = (ParamsBean) GsonUtil.getObject(result.getData(), ParamsBean.class);
//                        if (paramsBean != null) {
//                            registerContent = paramsBean.getUserRegisterProtocol();
//                        }
//                        break;
//                }
//                break;
//            case HandlerCode.FAIL:
//                hideProgress();
//                if (msg.obj != null) {
//                    showMessage(msg.obj.toString());
//                }
//                break;


        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.common_dialog_fragment_protcal;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            title = bundle.getString("title");
            content = bundle.getString("content");
            leftBtn = bundle.getString("leftBtn");
            rightBtn = bundle.getString("rightBtn");
            jumpType = bundle.getInt("type");
        }
//        getParam();
    }

//    private void getParam() {
//        UserApi.postMethod(handler, mContext, HandlerCode.GET_APP_PARAM, HandlerCode.GET_APP_PARAM, null, Urls.APP_PARAM, (BaseActivity) mContext);
//    }

    @Override
    protected void onInitView() {
        scrollView.setMaxHeight(ScreenUtil.getScreenHeight(getActivity()) / 4);
        SpannableStringBuilder style = new SpannableStringBuilder();

        //设置文字
        style.append("请您在使用之前点击阅读《用户协议》与《隐私政策》,如果同意，请点击下方“同意”按钮开启应用服务。");

        //设置用户协议文字点击事件
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {

                startActivity(new Intent(mContext, TextContentActivity.class)
                        .putExtra("title", "用户协议")
                        .putExtra("content", ReadAssetsUtil.getJson("userprotocol.txt", mContext)));

            }
        };
        style.setSpan(clickableSpan, 11, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        //设置部分文字颜色
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.parseColor("#80CCCC"));
        style.setSpan(foregroundColorSpan, 11, 17, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        //设置隐私政策文字点击事件
        ClickableSpan clickableSpan1 = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                startActivity(new Intent(mContext, TextContentActivity.class)
                        .putExtra("title", "隐私政策")
                        .putExtra("content", ReadAssetsUtil.getJson("agreement.txt", mContext)));
            }
        };
        style.setSpan(clickableSpan1, 18, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置部分文字颜色
        style.setSpan(foregroundColorSpan, 18, 24, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        //配置给TextView
        protcalDialogContent.setMovementMethod(LinkMovementMethod.getInstance());
        protcalDialogContent.setText(style);

        if (jumpType == 1) {
            getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK) {
                        getActivity().finish();
                        return false;
                    }
                    return false;
                }
            });
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);

        scrollView = rootView.findViewById(R.id.scroll_view);
        tvProtcalTitle = rootView.findViewById(R.id.tv_protcal_title);
        protcalDialogContent = rootView.findViewById(R.id.protcal_dialog_content);
        btnProtcalLeft = rootView.findViewById(R.id.btn_protcal_left);
        btnProtcalRight = rootView.findViewById(R.id.btn_protcal_right);

        btnProtcalLeft.setOnClickListener(v -> {
            //取消
            if (mOnOperationCancelListen != null) {
                mOnOperationCancelListen.setOperationCancel();
            }
            dismiss();
        });
        btnProtcalRight.setOnClickListener(v -> {
            if (mOnOperationConfirmListen != null) {
                mOnOperationConfirmListen.setOperationConfirm();
            }
            dismiss();
        });

        return rootView;
    }


    public void setOnOperationConfirmListen(OnOperationConfirmListen scoreListen) {
        mOnOperationConfirmListen = scoreListen;
    }


    public interface OnOperationConfirmListen {
        void setOperationConfirm();//确认
    }

    public void setOnOperationCancelListen(OnOperationCancelListen scoreListen) {
        mOnOperationCancelListen = scoreListen;
    }


    public interface OnOperationCancelListen {
        void setOperationCancel();//取消
    }
}
